package com.ebt.m.wiki;

import android.content.Context;

import com.ebt.m.AppContext;
import com.ebt.m.AppInitService;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.data.bean.TempDividerBrand;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.SearchHistory;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.buscomponent.listview.SectionItem;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wanglu on 2017/10/18.
 */

public class SpecProductPresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    private List<SectionItem> items = new ArrayList<>();
    public int mHistoryCount = 0;
    public int mBrandCount = 0;

    private Brand selectedBrand;
    private int from;

    public SpecProductPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        selectedBrand = (Brand) args[0];
        from = (int) args[1];
        EBTAPI ebtapi = AppInitService.getEbtApi();
        items.clear();
        mHistoryCount = 0;
        mBrandCount = 0;
        ebtapi.getSearchHistorys(from, 1, 1, 4)
                .subscribeOn(Schedulers.io())
                .doOnNext(listBaseDataResult -> {
                    List<SearchHistory> list = listBaseDataResult.getData();
                    if (list != null && list.size() > 0) {
                        SectionItem sectionItem = new SectionItem(SpecProductFilterView.TAG_VIEW_YTPE, "历史记录");
                        items.add(sectionItem);
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i).getCategory() == 1) {
                                Brand brand = new Brand();
                                Integer brandId = Integer.valueOf(list.get(i).getSearchId());
                                if (brandId == ConfigData.RECOMMEND_BRAND_ID) continue;
                                brand.setBrandId(brandId);
                                brand.setBrandShortName(list.get(i).getKeyword());
                                brand.setIsOpenInsurance(list.get(i).getIsOpenInsurance());
                                SectionItem item = new SectionItem(SpecProductFilterView.HISTORY_VIEW_TYPE, brand);
                                TempDividerBrand dividerBrand = new TempDividerBrand();
                                dividerBrand.setShowDivider(true);
                                item.extras = dividerBrand;
                                items.add(item);
                                if (items.size() == 4) break;
                            }
                        }
                        if (items.size() == 1) {
                            items.clear();
                        } else {
                            TempDividerBrand dividerBrand = (TempDividerBrand) (items.get(items.size() - 1).extras);
                            dividerBrand.setShowDivider(false);
                        }
                        mHistoryCount = items.size() - 1;
                    }
                })
                .flatMap(listBaseDataResult -> ebtapi.getBrandsByCompanyId(AppContext.getCurrentUser().getCompanyInfo().getCompanyID()))
                .doOnNext(searchCategorys -> {
                    SectionItem sectionItem = new SectionItem(SpecProductFilterView.TAG_VIEW_YTPE, "全部品牌");
                    items.add(sectionItem);
                    if (from == 2) {
                        Brand recommendBrand = new Brand();
                        recommendBrand.setBrandId(ConfigData.RECOMMEND_BRAND_ID);
                        recommendBrand.setBrandName("推荐产品");
                        recommendBrand.setBrandShortName("推荐产品");
                        SectionItem sectionItem1 = new SectionItem(SpecProductFilterView.BRAND_VIEW_TYPE, recommendBrand);
                        TempDividerBrand dividerBrand = new TempDividerBrand();
                        dividerBrand.setShowDivider(true);
                        dividerBrand.setSelectedBrand(selectedBrand);
                        sectionItem1.extras = dividerBrand;
                        items.add(sectionItem1);
                    }
                    List<Brand> data = searchCategorys.getData();
                    if (data != null && data.size() > 0) {
                        for (Brand brand : data) {
                            SectionItem item = new SectionItem(SpecProductFilterView.BRAND_VIEW_TYPE, brand);
                            TempDividerBrand dividerBrand = new TempDividerBrand();
                            dividerBrand.setShowDivider(true);
                            dividerBrand.setSelectedBrand(selectedBrand);
                            item.extras = dividerBrand;
                            items.add(item);
                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sectionItems ->
                                iView.updateData(items)
                        , throwable -> {
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew(selectedBrand, from);
    }

    @Override
    public void loadNext(Object... args) {
    }

}
